home *** CD-ROM | disk | FTP | other *** search
- Path: Belgium.EU.net!box!pahint
- From: pahint@eunet.be (Pieter Hintjens)
- Newsgroups: comp.lang.c
- Subject: Universal include file
- Date: 24 Feb 1996 17:55:22 GMT
- Organization: EUnet Belgium, Leuven, Belgium
- Message-ID: <4gnjea$30m@news.Belgium.EU.net>
- NNTP-Posting-Host: box.eunet.be
- X-Newsreader: TIN [version 1.2 PL2]
-
- I am working on a project to build a 'Universal Include File'.
- The point is to encapsulate the peculiarities of every system
- under the sun into one include file. Maybe this is not doable,
- but I already have something halfway decent.
-
- The UIF should include all the standard header files: certainly
- all those defined in the ANSI C standard, plus those that are
- common enough to be valuable. (E.g. socket interface files.)
-
- The objective is to build more portable C programs without the
- usual mess of #ifdef's.
-
- This is the file that I've been using for a few years now... I
- hope it shows the general direction. Forgive a few idiosyncratic
- macros. ;-)
-
- Any comments and specific information gratefully received!
-
- Cheers,
- Pieter A. Hintjens
-
-
- /*===========================================================================*
- * *
- * prelude.h Universal header file for C programming *
- * *
- * Written: 93/03/29 Pieter Hintjens *
- * Revised: 96/02/22 *
- * *
- * This header encapsulates all generally-useful include files and defines *
- * various useful things. To use, specify as first include file in code. *
- * Under UNIX, you should use the 'c' compile script; this defines the *
- * _UNIX_TYPE symbol, needed to get system-specific #include's correct. *
- * *
- * Author: Pieter A. Hintjens *
- * Pijlstraat 9 *
- * 2060 Antwerpen, Belgium *
- * ph@mymail.com *
- * (+323) 231.5277 *
- * *
- * Copyright (c) 1991-96 Pieter A. Hintjens. May be freely distributed. *
- *===========================================================================*/
-
- #ifndef _PRELUDE_INCLUDED /* Allow multiple inclusions */
- #define _PRELUDE_INCLUDED
-
-
- /*- Generally-useful include files ------------------------------------------*/
-
- #include <ctype.h>
- #include <limits.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <signal.h>
- #include <errno.h>
- #include <fcntl.h>
-
-
- /*- Establish the compiler and computer system ------------------------------*/
- /*
- * Defines one or more of these symbols, for use in any non-portable
- * code:
- *
- * __WINDOWS__ Microsoft C/C++ with Windows calls
- * __MSDOS__ File system is MS-DOS
- * __VMS__ System is VAX/VMS or Alpha/OpenVMS
- * __UNIX__ System is UNIX
- * __MAC__ System is Mac/OS
- *
- * __32BIT__ OS/compiler is 32 bits
- * __64BIT__ OS/compiler is 64 bits (only Digital UNIX so far)
- */
-
- #define __32BIT__ /* Assume 32-bit OS/compiler */
-
- #if (defined (WIN32) || defined (WINDOWS))
- # undef __WINDOWS__
- # define __WINDOWS__
- # undef __MSDOS__
- # define __MSDOS__
- #endif
-
- /* MSDOS is Microsoft C */
- /* _MSC_VER is Microsoft C */
- /* __TURBOC__ is Borland Turbo C */
- #if (defined (MSDOS) || defined (_MSC_VER) || defined (__TURBOC__))
- # undef __MSDOS__
- # define __MSDOS__
- #endif
-
- /* VMS is VAX C (VAX/VMS) */
- /* __VMS is Dec C (Alpha/OpenVMS) */
- /* __vax__ is gcc */
- #if (defined (VMS) || defined (__VMS) || defined (__vax__))
- # undef __VMS__
- # define __VMS__
- #endif
-
- /* unix is SunOS */
- /* __unix__ is gcc */
- /* _POSIX_SOURCE is various UNIX systems, maybe also VAX/VMS */
- #if (defined (unix) || defined (__unix__) || defined (_POSIX_SOURCE))
- # if !defined (__VMS__)
- # undef __UNIX__
- # define __UNIX__
- # if (defined (__alpha)) /* Digital UNIX is 64-bit */
- # undef __32BIT__
- # define __64BIT__
- # endif
- # endif
- #endif
-
- /* Untested */
- #if (defined (_MAC))
- # undef __MAC__
- # define __MAC__
- #endif
-
-
- /*- System-specific include files -------------------------------------------*/
-
- #if (defined (__TURBOC__)) /* Borland Turbo-C uses this */
- # include <alloc.h> /* name for its include file */
- #else
- # include <malloc.h>
- #endif
-
- #if (defined (__WINDOWS__)) /* When __WINDOWS__ is defined, */
- # include <windows.h> /* so is __MSDOS__ */
- #endif
-
- #if (defined (__MSDOS__))
- # include <dos.h>
- # include <io.h>
- # include <fcntl.h>
- # include <sys\stat.h>
- # include <sys\types.h>
- #endif
-
- #if (defined (__UNIX__))
- # include <netdb.h>
- # include <unistd.h>
- # include <netinet/in.h>
- # include <sys/param.h>
- # include <sys/socket.h>
- # include <sys/time.h>
- # include <sys/stat.h>
- # include <sys/types.h>
- /* Specific includes; the c script defines one of:
- * _UNIX_AIX _UNIX_APOLLO _UNIX_A_UX
- * _UNIX_BS_DOS _UNIX_HP_UX _UNIX_IRIX
- * _UNIX_LINUX _UNIX_NCR _UNIX_NETBSD
- * _UNIX_NEXT _UNIX_OSF1 _UNIX_SCO
- * _UNIX_PYRAMID _UNIX_SUNOS _UNIX_ULTRIX
- * _UNIX_GENERIC
- */
- # if defined (_UNIX_AIX) /* Specific includes for IBM/AIX */
- # include <sys/select.h>
- # endif
- #endif
-
- #if (defined (__VMS__))
- # include <netdb.h>
- # include <unixio.h>
- # include <in.h>
- # include <param.h>
- # include <socket.h>
- # include <time.h>
- # include <stat.h>
- # include <types.h>
- #endif
-
-
- /*- Data types --------------------------------------------------------------*/
-
- typedef unsigned char byte;
- typedef unsigned short word;
- typedef int Bool;
- #if (defined (__32BIT__))
- typedef unsigned long dword; /* Long is 32 bits */
- typedef long int lint;
- #else
- typedef unsigned int dword; /* Long is 64 bits, int is 32 bits */
- typedef int lint;
- #endif
- typedef void (*function) (void); /* Address of simple function */
- #define local static void /* Shorthand for local functions */
-
-
- /*- Check compiler data type sizes ------------------------------------------*/
-
- #if (UCHAR_MAX != 0xFF)
- # error "Cannot compile: must change definition of 'byte'."
- #endif
- #if (USHRT_MAX != 0xFFFFU)
- # error "Cannot compile: must change definition of 'word'."
- #endif
- #if (defined (__32BIT__))
- # if (ULONG_MAX != 0xFFFFFFFFUL)
- # error "Cannot compile: must change definition of 'dword'."
- # endif
- #else
- # if (UINT_MAX != 0xFFFFFFFFU)
- # error "Cannot compile: must change definition of 'dword'."
- # endif
- #endif
-
-
- /*- Pseudo-functions --------------------------------------------------------*/
-
- #define ever (;;) /* for ever { ... } */
- #define until(expr) while (!(expr)) /* do { ... } until (expr) */
- #define error(expr) ((expr) < 0) /* if error (expr) { ... */
- #define loop(var,n) for (var = 0; var < n; var++)
-
- #define strlast(str) (str [strlen (str) - 1])
- #define strterm(str) (str [strlen (str))
- #define streq(s1, s2) (!strcmp ((s1), (s2)))
- #define strneq(s1, s2) (strcmp ((s1), (s2)))
- #define strused(s) (*(s) != 0)
- #define strnull(s) (*(s) == 0)
- #define strclr(s) (*(s) = 0)
-
- #define bit_msk(bit) (1 << (bit))
- #define bit_set(x, bit) ((x) |= bit_msk (bit))
- #define bit_clr(x, bit) ((x) &= ~bit_msk (bit))
- #define bit_tst(x, bit) ((x) & bit_msk (bit))
-
- #define tblsize(x) (sizeof (x) / sizeof ((x) [0]))
- #define tbllast(x) (x [tblsize (x) - 1]);
-
- #if (defined (random))
- # undef random
- # undef randomize
- #endif
- #if (defined (min))
- # undef min
- # undef max
- #endif
-
- #if (defined (__32BIT__))
- #define random(num) (int) ((long) rand () % (num))
- #else
- #define random(num) (int) ((int) rand () % (num))
- #endif
- #define randomize() srand ((unsigned) time (NULL))
- #define min(a,b) (((a) < (b))? (a): (b))
- #define max(a,b) (((a) > (b))? (a): (b))
-
-
- /*- ASSERT and debugging ----------------------------------------------------*/
-
- #if (defined (DEBUG))
- /* Define _Assert function here locally */
- local _Assert (char *File, unsigned Line)
- {
- fflush (stdout);
- fprintf (stderr, "\nAssertion failed: %s, line %u\n", File, Line);
- fflush (stderr);
- abort ();
- }
- # define ASSERT(f) \
- if (f) \
- ; \
- else \
- _Assert (__FILE__, __LINE__)
- #else
- # define ASSERT(f)
- #endif
-
-
- /*- Boolean operators and constants -----------------------------------------*/
-
- #if (defined (TRUE))
- # undef TRUE
- # undef FALSE
- #endif
- #define TRUE (1==1)
- #define FALSE (1==0)
- #define AND &&
- #define OR ||
-
-
- /*- Symbolic constants ------------------------------------------------------*/
-
- #define FORK_ERROR -1 /* Return codes from fork() */
- #define FORK_CHILD 0
- #if (!defined (LINE_MAX))
- # define LINE_MAX 255
- #endif
- #define TAB '\t'
-
-
- /*- System-specific definitions ---------------------------------------------*/
-
- #if (defined (__VMS__))
- # define RET_OKAY 1 /* Return codes for main() */
- # define RET_ERROR 0 /* VMS does it its own way. */
- #else
- # define RET_OKAY 0 /* On non-VMS systems 0 is okay, */
- # define RET_ERROR 1 /* and 1 is an error. */
- #endif
-
- #endif /* Include PRELUDE.H */
-